﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StoragePeriod.IDAL.FoodTesting;
using StoragePeriod.Model.FoodTesting;
using System.Data;
using StoragePeriod.DBUtility;
using System.Data.SqlClient;

namespace StoragePeriod.SqlServerDAL.FoodTesting
{
    /// <summary>
    /// 水分含量表 接口
    /// </summary>
    public class D_WaterData : I_WaterData
    {
        /// <summary>
        /// 获取检查数据实体
        /// </summary>
        /// <param name="eVcData">查询ID</param>
        /// <returns>返回对应的实体</returns>
        public E_WaterData GetDataEntityById(E_WaterData eWaterData)
        {
            StringBuilder sbSql = new StringBuilder();
            sbSql.Append("select * from WaterData where id=" + eWaterData.id);
            DataTable dt = DbHelperSQL.Query(sbSql.ToString()).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                E_WaterData Entity = new ModelHandler<E_WaterData>().FillModel(dt.Rows[0]);
                return Entity;
            }
            return null;
        }

        /// <summary>
        /// 根据原料ID和检测时间 删除检测记录
        /// </summary>
        /// <param name="eVcData">要删除的检测记录</param>
        /// <returns></returns>
        public int RemoveByTime(E_WaterData eWaterData)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from WaterData where RawId=" + eWaterData.RawId + " and WaterTime=CAST('" + eWaterData.WaterTime.ToString() + "' as datetime)");
            return DbHelperSQL.ExecuteSql(strSql.ToString());
        }

        /// <summary>
        /// 删除某一次检测记录
        /// </summary>
        /// <param name="eVcData">要删除的检测记录</param>
        /// <returns></returns>
        public int RemoveByFoodNum(E_WaterData eWaterData)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from WaterData where RawId=" + eWaterData.RawId + " and WaterTime=CAST('" + eWaterData.WaterTime.ToString() + "' as datetime) and FoodNum=" + eWaterData.FoodNum);
            return DbHelperSQL.ExecuteSql(strSql.ToString());
        }

        /// <summary>
        /// 根据原料ID 和检测时间 获取所有的检测记录
        /// </summary>
        /// <param name="eVcData">要获取的检测记录</param>
        /// <returns></returns>
        public List<E_WaterData> GetDataListByTime(E_WaterData eWaterData)
        {
            StringBuilder sbSql = new StringBuilder();
            sbSql.Append("select * from WaterData where RawId=" + eWaterData.RawId + " and WaterTime=cast('" + eWaterData.WaterTime.ToString() + "' as datetime)");
            DataTable dt = DbHelperSQL.Query(sbSql.ToString()).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                List<E_WaterData> EntityList = new ModelHandler<E_WaterData>().FillModel(dt);
                return EntityList;
            }
            return null;
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(E_WaterData eWaterData)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into WaterData(");
            strSql.Append("RawId,RawName,RawPosition,Water_m0,Water_m1,Water_m2,Water_m1_S,Water_m2_S,WaterContent,WaterTime,FoodNum)");
            strSql.Append(" values (");
            strSql.Append("@RawId,@RawName,@RawPosition,@Water_m0,@Water_m1,@Water_m2,@Water_m1_S,@Water_m2_S,@WaterContent,@WaterTime,@FoodNum)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@RawId", SqlDbType.Int,4),
					new SqlParameter("@RawName", SqlDbType.VarChar,50),
					new SqlParameter("@RawPosition", SqlDbType.VarChar,50),
					new SqlParameter("@Water_m0", SqlDbType.VarChar,50),
					new SqlParameter("@Water_m1", SqlDbType.VarChar,50),
					new SqlParameter("@Water_m2", SqlDbType.VarChar,50),
					new SqlParameter("@Water_m1_S", SqlDbType.VarChar,50),
					new SqlParameter("@Water_m2_S", SqlDbType.VarChar,50),
					new SqlParameter("@WaterContent", SqlDbType.VarChar,50),
					new SqlParameter("@WaterTime", SqlDbType.DateTime),
					new SqlParameter("@FoodNum", SqlDbType.Int,4)};
            parameters[0].Value = eWaterData.RawId;
            parameters[1].Value = eWaterData.RawName;
            parameters[2].Value = eWaterData.RawPosition;
            parameters[3].Value = eWaterData.Water_m0;
            parameters[4].Value = eWaterData.Water_m1;
            parameters[5].Value = eWaterData.Water_m2;
            parameters[6].Value = eWaterData.Water_m1_S;
            parameters[7].Value = eWaterData.Water_m2_S;
            parameters[8].Value = eWaterData.WaterContent;
            parameters[9].Value = eWaterData.WaterTime;
            parameters[10].Value = eWaterData.FoodNum;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
    }
}
